<?php
/**
 * 服务器核心入口类
 */

class server 
{
	var $var = array();
	var $init_db = true;
	var $init_user = false;
	
	function server() 
	{
		if(!defined('IN_SERVER')) 
		{
			$this->_init_env();
			$this->_init_config();
		}
	}

	function init() 
	{
		if(!$this->var['initiated']) 
		{
			$this->_init_db();
			$this->var['initiated'] = true;
		}
	}

	function run($type='') 
	{		
		$GameData =array();
		$GameData['data'] =array();
		$GameData['event'] =array();
		
		$playerToken ="";
		$playerAccount ="";
		
		$this->var['rc'] =0;
		$this->var['warring_msg_id'] ="";
		$this->var['timestamp'] =time();
		$this->var['request_url'] =$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		
		$types = array(
			'index'=>array('mod_default'=>$this->var['config']['default_module'] ? $this->var['config']['default_module'] : 'topic'),
		);
		
		if(!isset($types[$type])) { $type = 'index'; }
		
		$modules_path = ROOT_PATH . ($types[$type]['mod_path'] ? $types[$type]['mod_path'] : ('s_modules/' . ('index' == $type ? '' : $type . '/')));
		if(!(@include_once $modules_path . 'master.mod.php') && !class_exists('MasterObject')) 
		{
			$this->var['rc'] =1;
			$this->var['warring_msg_id'] ="warning_id_001";
		}
		else 
		{
			if(!(include $modules_path . ($this->_init_mod($types[$type])) . '.mod.php') && !class_exists('ModuleObject'))
			{
				$this->var['rc'] =1;
				$this->var['warring_msg_id'] ="warning_id_002";
			}
			else
			{
				$this->init();
				$ModuleObject = new ModuleObject($this->var);
				
				$GameData['data'] =$ModuleObject->GameData();
				$GameData['event'] =$ModuleObject->GameEvent();
				$GameData['event']["abc"] ="abc";
				
				$playerToken =$ModuleObject->playerToken;
				$playerAccount =$ModuleObject->playerAccount;
			}
		}

		if($this->var['object_db'] !=null) $this->var['object_db']->CloseConnection();
		
		$GameData['rc'] =$this->var['rc'];
		$GameData['mc'] =$this->var['warring_msg_id'];
		$GameData['timetamp'] =$this->var['timestamp'];
		
		//create json
		$json_data =trim(json_encode($GameData));
		echo $json_data;
		
		//insert log
		$game_log = array(
				'token'		=> $playerToken,
				'account' 	=> $playerAccount,
				'url' 		=> $this->var['request_url'],
				'data' 		=> $json_data,
				'timestamp' 	=> strtotime("now"),
				'submit_time' 	=> date("Y-m-d H:i:s",time()),
				'submit_ip' 	=> GetIP(),
		);
		DB::insert('sys_log', $game_log, true);
	}

	function _init_env() 
	{
		error_reporting(E_ERROR);
		@set_time_limit(300);
		if(PHP_VERSION < '5.3.0') 
		{
			set_magic_quotes_runtime(0);
		}

		define('IN_SERVER', true);
		define('ROOT_PATH', substr(dirname(__FILE__), 0, -9) . '/');
		
		define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
		define('TIMESTAMP', time());
		
		if(!defined('SERVER_GLOBAL_FUNCTION') && !@include(ROOT_PATH . 's_include/global.func.php')) 
		{
			exit('global.func.php is not exists');
		}
		
		if(function_exists('ini_set')) 
		{
			ini_set('memory_limit', '256M');
			ini_set('max_execution_time', 300);
			ini_set('arg_seperator.output', '&amp;');
			ini_set('magic_quotes_runtime', 0);
		}

		$superglobal = array(
				'GLOBALS' => 1,
				'_GET' => 1,
				'_POST' => 1,
				'_COOKIE' => 1,
				'_SERVER' => 1,
				'_FILES' => 1,
		);
		foreach($GLOBALS as $k=>$v) 
		{
			if(!isset($superglobal[$k])) 
			{
				$GLOBALS[$k] = null; 
				unset($GLOBALS[$k]);
			}
		}

		global $_J;
		$_J = array(
			'player_account' =>'',
			'player_token' => '',
			'player_name' => '',
			'player_level' => 0,
			'player_money' => '',
			'player_gold' => '',
			'player_sex' => '',
			'mod' =>'',
			'code' =>'',
			'method'=>'',
			'class'=>'',
			'processing'=>'',
			'rc' =>0,
			'warring_msg_id' =>'',
				
			'object_db' =>null,
			'initiated' =>false,
			'timestamp' => TIMESTAMP,
			'time_start' => microtime(true),
		);
		
		$this->var = & $_J;
	}

	function _init_config() 
	{
		$config = ConfigHandler::get();
		
		define('CHARSET', $config['charset']);
		@header('Content-Type: text/html; charset=' . CHARSET);
		@header('P3P: CP="CAO PSA OUR"');

		require ROOT_PATH . 's_setting/constants.php';
		$config['sys_version'] = sys_version();
		$config['sys_published'] = SYS_PUBLISHED;
		
		Obj::register('config', $config);	
		$load_configs = array('modules');
		foreach($load_configs as $k) 
		{
			$config[$k] = ConfigHandler::get($k);
		}
		
		$this->var['config'] = & $config;
	}

	function _init_mod($options = array()) 
	{
		$mod_default = ($options['mod_default'] ? $options['mod_default'] : 'index');
		$method = $this->_get('method');
		
		$list =explode('.', $method);
		if(count($list) < 2)
		{
			$this->var['rc'] =1;
			$this->var['warring_msg_id'] ="warring_id004";
			return ;
		}
			
		$this->var['class'] = $list[0];
		$this->var['processing'] = $list[1];
		
		if(!isset($this->var['config']['modules'][$this->var['class']])) 
		{
			$this->var['rc'] =1;
			$this->var['warring_msg_id'] ="warring_id004";
			return ;
		}
		
		$_POST['method'] = $_GET['method'] = $method;
		define('CUR_METHOD', $method);
		
		return $this->var['class'];
	}

	function _init_db() 
	{
		if($this->init_db) 
		{
			include_once ROOT_PATH . 's_include/db/database.db.php';
			include_once ROOT_PATH . 's_include/db/mysql.db.php';
			$this->var['object_db'] = new MySqlHandler();
			$this->var['object_db']->connect();
			
			Obj::register('DatabaseHandler', $this->var['object_db']);
		}
	}

	function _get($var, $ifemptyval = null) 
	{
		$val = jget($var);
		if(!$val) 
		{
			$val = $_REQUEST[$var];
		}
		if(empty($val)) 
		{
			return (is_null($ifemptyval) ? $val : $ifemptyval);
		} 
		else 
		{
			return str_safe($val);
		}
	}
}

?>